{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b240b0dd",
   "metadata": {},
   "source": [
    "## 列表一般方法\n",
    "> 1. 增加\n",
    "> 2. 删除\n",
    "> 3. 改\n",
    "> 4. 查 ：in"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb685850",
   "metadata": {},
   "source": [
    "|  方法   | Description |\n",
    "| :-----------: | :-----------: |\n",
    "| append() | 追加一个元素 |\n",
    "| extend() | 追加多个元素 |\n",
    "| insert() | 在指定索引位置插入元素 |\n",
    "| pop() | 弹出元素并删除 |\n",
    "| remove() | 删除指定对象名字 |\n",
    "| del | 删除列表 |\n",
    "| count() | 查看出现的次数 |\n",
    "| index() | 查看指定元素的索引值 |\n",
    "| sort() |分类排序|\n",
    "|reverse()|将列表进行翻转|\n",
    "|copy()|把前面的列表复制一遍|"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4af949a7",
   "metadata": {},
   "source": [
    "## 1.增加\n",
    "往列表里增加元素\n",
    "> 1. append()：增加一个元素\n",
    "> 2. extend()：追加多个元素\n",
    "> 3. insert()：在指定索引位置插入元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "02247ded",
   "metadata": {},
   "outputs": [],
   "source": [
    "name = ['Python','信息可视化','数字媒体技术']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e938c256",
   "metadata": {},
   "outputs": [],
   "source": [
    "name.append('电子商务概论')     # append:增补 追加一个元素到列表中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "33e0a730",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Python', '信息可视化', '数字媒体技术', '电子商务概论']\n"
     ]
    }
   ],
   "source": [
    "print(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "bfb2be47",
   "metadata": {},
   "outputs": [],
   "source": [
    "name.extend(['瑜伽','大英'])  # extend:拉伸 追加多个元素到列表中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ee6be899",
   "metadata": {},
   "outputs": [],
   "source": [
    "name.insert(1,'100分')    # insert：在指定索引位置插入元素\n",
    "                          # 在第二个元素的位置插入100分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "9ebb2f9a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Python', '100分', '信息可视化', '数字媒体技术', '电子商务概论', '瑜伽', '大英']\n"
     ]
    }
   ],
   "source": [
    "print(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "0cffd7be",
   "metadata": {},
   "outputs": [],
   "source": [
    "name.insert(3,'92分')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ddf7b235",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Python', '100分', '信息可视化', '92分', '数字媒体技术', '电子商务概论', '瑜伽', '大英']\n"
     ]
    }
   ],
   "source": [
    "print(name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6299151c",
   "metadata": {},
   "source": [
    "## 2.删除\n",
    "> 1. pop()：弹出元素并删除\n",
    "> 2. remove()：删除指定对象名字（直接删除，不能将其赋值）\n",
    "> 3. del：删除列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "506266ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'大英'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name.pop()    # 弹出最后一个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "17e23f51",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = name.pop(0)   # 弹出第一个元素 # 可以将其赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "a07bc8d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'100分'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name.pop(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "94424805",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python\n"
     ]
    }
   ],
   "source": [
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "b1ce4194",
   "metadata": {},
   "outputs": [],
   "source": [
    "name.remove('数字媒体技术')  # remove：删除指定对象名字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "b8663310",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['信息可视化', '92分', '电子商务概论', '瑜伽']\n"
     ]
    }
   ],
   "source": [
    "print(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "ea509779",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'name' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [25]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mdel\u001b[39;00m name\n",
      "\u001b[1;31mNameError\u001b[0m: name 'name' is not defined"
     ]
    }
   ],
   "source": [
    "del name  # 删除列表"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b618cf6",
   "metadata": {},
   "source": [
    "## 3.赋值（改）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "078a06a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "name = ['python','信息可视化','数字媒体技术']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "641e0504",
   "metadata": {},
   "outputs": [],
   "source": [
    "name[0] = '大数据'   # 通过索引，重新赋值（换值）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "64be9672",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['大数据', '信息可视化', '数字媒体技术']"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "fd24f766",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['大数据', '信息可视化', '数字媒体技术']\n"
     ]
    }
   ],
   "source": [
    "print(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "595faad9",
   "metadata": {},
   "outputs": [],
   "source": [
    "name[:2] = ['体育','瑜伽']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "20eb0f1c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['体育', '瑜伽', '数字媒体技术']"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9eb8d137",
   "metadata": {},
   "source": [
    "## 4.查看\n",
    "> 1. count()：查看出现的次数\n",
    "> 2. index()：查看指定元素的索引值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "dae33e06",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name.count('大英')   # count():查看出现的次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "9a60c537",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name.count('体育')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "e419e0cb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name.index('瑜伽')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "4ed9531e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name.index('瑜伽',1,3) \n",
    "# 从1-3中查找（第二个元素和第三个元素之间）\n",
    "#（不取上限）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "83ef4101",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name.index('数字媒体技术',1,3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c9423c8",
   "metadata": {},
   "source": [
    "## 5.排序\n",
    "> sort()：分类排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "9e61fbb6",
   "metadata": {},
   "outputs": [],
   "source": [
    "name = ['alex', 'Bob', 'Ccd']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "574ae84f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Bob', 'Ccd', 'alex']"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name.sort()    # 按照ASCLL排序\n",
    "name      # 先排序首字母为答谢的，在排序首字母是小写的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "490fcdf7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['alex', 'Bob', 'Ccd']"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name.sort(key=str.lower) # 对字符串排序不区分大小写，\n",
    "name             # 相当于将所有元素转换为小写，再排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "e533802c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['alex', 'Bob', 'Ccd']"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name.sort(key=str.upper) # 相当于将所有元素转换为大写，\n",
    "name                       # 再排序"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02bc41fd",
   "metadata": {},
   "source": [
    "## 6.乱序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "fb1c0e3f",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = list(range(10))  # 生成0-9，将其转换为列表形式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "8ceed0e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
     ]
    }
   ],
   "source": [
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "63a16131",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[9, 1, 3, 4, 2, 6, 8, 0, 5, 7]\n"
     ]
    }
   ],
   "source": [
    "import random   # random：随机  shuffle：打乱\n",
    "random.shuffle(a)  # 随机打乱\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4764e078",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
